package com.gouaixin.bee.beeframework2.fragment;



import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.adapter.mFragmentPagerAdapter;

import java.util.ArrayList;


/**
 * Created by Administrator on 2015/3/6.
 */
public class M1_indexFragment extends Fragment {
    Context context=getActivity();
    //主布局
    View mainView;

    //内容
    public static ViewPager viewpager;
    private mFragmentPagerAdapter adapter;
    private int currentPageIndex = 0;
    private ArrayList<Fragment> datas;

    LinearLayout[] llArray;//tab选项
    TextView[] bgArray;//tab背景


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mainView= inflater.inflate(R.layout.m1_index, null);

        initViewPager();
//        initFrameLayout();
        initTab();

        return mainView;
    }




    /****
     * ViewPager的设置和监听
     */
    private void initViewPager() {
        viewpager = (ViewPager)mainView.findViewById(R.id.index_viewPager);
//        adapter = new mFragmentPagerAdapter(getActivity().getSupportFragmentManager(), null);


        FragmentManager fm=getChildFragmentManager();
        adapter = new mFragmentPagerAdapter(fm, null);



        datas = new ArrayList<Fragment>();

        datas.add(new I0_tuijianFargment());
        datas.add(new I0_zuixinFargment());
        datas.add(new I0_kanguoFargment());



        adapter.setDatas(datas);
        adapter.notifyDataSetChanged();

        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(0);
        viewpager .setOffscreenPageLimit(datas.size()-1);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int index) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int pageIndex) {

                currentPageIndex = pageIndex;

                //根据页面数设置Tab背景
                setTabColor();

            }
        });

    }

    /****
     * Tab按钮的设置和监听
     */
    private void initTab() {

        llArray=new LinearLayout[]{
                (LinearLayout) mainView.findViewById(R.id.tuijianLl),
                (LinearLayout) mainView. findViewById(R.id.zuixinLl),
                (LinearLayout) mainView. findViewById(R.id.kanguoLl),
        };

        bgArray=new TextView[]{
                (TextView) mainView.findViewById(R.id.tuijianBg),
               (TextView) mainView. findViewById(R.id.zuixinBg),
               (TextView) mainView. findViewById(R.id.kanguoshuiBg),

        };

        for (LinearLayout btn : llArray) {
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        switch (v.getId()) {
                            case R.id.tuijianLl:
                                currentPageIndex = 0;



                                break;
                            case R.id.zuixinLl:
                                currentPageIndex = 1;


                                break;
                            case R.id.kanguoLl:
                                currentPageIndex = 2;

                                break;
                        }
                        viewpager.setCurrentItem(currentPageIndex);
                        android.util.Log.e("Demo", "顶部Tab:currentPageIndex="+currentPageIndex);
                    } catch (Exception e) {
                        e.printStackTrace();
                        android.util.Log.e("Demo", "顶部Tab:出问题了="+e.getMessage());
                    }
                }
            });
        }

    }


    /**
     * 根据currentPageIndex设置Tab按钮的背景
     */
    private void setTabColor() {
        for (int i = 0; i < bgArray.length; i++) {
            if (i == currentPageIndex) {
                bgArray[i].setVisibility(View.VISIBLE);
            } else {
                bgArray[i].setVisibility(View.INVISIBLE);
            }

        }
    }

}
